package org.basic.mail.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.basic.common.mybatis.core.page.TableDataInfo;
import org.basic.mail.domain.bo.SysMailLogBo;
import org.basic.mail.domain.vo.SysMailLogVo;
import org.basic.mail.service.ISysMailLogService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.basic.common.idempotent.annotation.RepeatSubmit;
import org.basic.common.log.annotation.Log;
import org.basic.common.web.core.BaseController;
import org.basic.common.mybatis.core.page.PageQuery;
import org.basic.common.core.domain.R;
import org.basic.common.core.validate.AddGroup;
import org.basic.common.core.validate.EditGroup;
import org.basic.common.log.enums.BusinessType;
import org.basic.common.excel.utils.ExcelUtil;

/**
 * 邮件日志
 *
 * @author Shao Hua
 * @date 2023-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/mailLog")
public class SysMailLogController extends BaseController {

    private final ISysMailLogService sysMailLogService;

    /**
     * 查询邮件日志列表
     */
    @SaCheckPermission("system:mailLog:list")
    @GetMapping("/list")
    public TableDataInfo<SysMailLogVo> list(SysMailLogBo bo, PageQuery pageQuery) {
        return sysMailLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮件日志列表
     */
    @SaCheckPermission("system:mailLog:export")
    @Log(title = "邮件日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysMailLogBo bo, HttpServletResponse response) {
        List<SysMailLogVo> list = sysMailLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮件日志", SysMailLogVo.class, response);
    }

    /**
     * 获取邮件日志详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:mailLog:query")
    @GetMapping("/{id}")
    public R<SysMailLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(sysMailLogService.queryById(id));
    }

    /**
     * 新增邮件日志
     */
    @SaCheckPermission("system:mailLog:add")
    @Log(title = "邮件日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysMailLogBo bo) {
        return toAjax(sysMailLogService.insertByBo(bo));
    }

    /**
     * 修改邮件日志
     */
    @SaCheckPermission("system:mailLog:edit")
    @Log(title = "邮件日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysMailLogBo bo) {
        return toAjax(sysMailLogService.updateByBo(bo));
    }

    /**
     * 删除邮件日志
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:mailLog:remove")
    @Log(title = "邮件日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysMailLogService.deleteWithValidByIds(List.of(ids), true));
    }
}
